package com.sc.userspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.userspringboot.pojo.Comments;
import com.sc.userspringboot.pojo.Posts;
import com.sc.userspringboot.service.*;
import com.sc.userspringboot.dto.PostPage;
import com.sc.userspringboot.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/postDetail")
public class HYJ_postDetail {

    @Autowired
    private IUsersService iUsersService;//用户
    @Autowired
    private IPostsService iPostsService;//帖子
    @Autowired
    private IGamesService iGamesService;//游戏
    @Autowired
    private IPostTagsService iPostTagsService;//帖子标签
    @Autowired
    private IPostImagesService iPostImagesService;//帖子图片
    @Autowired
    private IPostCollectionsService iPostCollectionsService;//帖子收藏
    @Autowired
    private ICommentsService iCommentsService;//帖子评论
    //获取帖子详情
    @RequestMapping("getDetail")
    public ResponseMsg getDetail(@RequestBody Integer postId) {

        Posts posts=iPostsService.getById(postId);
        posts.setUser(iUsersService.getById(posts.getUserId()));//找到对应作者
        posts.setPostImages(iPostImagesService.getByPostId(posts.getPostId()));//找到图片
        posts.setGames(iGamesService.getById(posts.getGameId()));//对应游戏关联
        IPage<Comments> ipage = new Page<>(1, 6);
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id",postId)
                .isNull("parent_comment_id");//一级评论
        List<Comments> comments = iCommentsService.list(ipage,queryWrapper);
        for (Comments comment : comments) {
            comment.setUser(iUsersService.getById(comment.getUserId()));//找到对应作者
        }

        posts.setComments(comments);//一级评论
        posts.setCommentCount((int) ipage.getTotal());//一级评论数量
//关联标签没写
        return ResponseMsg.getInstance(200, "数据获取取成功",posts );

    }

    @RequestMapping("getMoreComm/{postId}")//加载更多评论
    public ResponseMsg getMoreComm(@PathVariable Integer postId,@RequestBody PostPage postPage) {
        Integer page=postPage.getPage();
        Integer limit=postPage.getLimit();
        List<Comments> comments;
        try {
            IPage<Comments> ipage = new Page<>(page, limit);
            QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("post_id", postId)
                    .isNull("parent_comment_id");//一级评论
            comments = iCommentsService.list(ipage, queryWrapper);
            for (Comments comment : comments) {
                comment.setUser(iUsersService.getById(comment.getUserId()));//找到对应作者
            }
        }catch (Exception e){
            return ResponseMsg.getInstance(500, "数据获取失败",null );
        }
        return ResponseMsg.getInstance(200, "数据获取取成功",comments);

    }


}
